package org.adorsys.aderp.orga.web;

import java.util.List;

import org.adorsys.aderp.orga.domain.OrgaResource;
import org.adorsys.aderp.orga.domain.ResResRelation;
import org.adorsys.aderp.orga.domain.ResourceAddress;
import org.adorsys.aderp.orga.domain.ResourceEmail;
import org.adorsys.aderp.orga.domain.ResourceRelation;
import org.adorsys.aderp.orga.service.ResResRelationService;
import org.adorsys.aderp.orga.service.ResourceAddressService;
import org.adorsys.aderp.orga.service.ResourceEmailService;
import org.adorsys.aderp.orga.service.ResourceRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/orgaresources")
@Controller
@RooWebScaffold(path = "orgaresources", formBackingObject = OrgaResource.class)
public class OrgaResourceController {
  
	@Autowired
	private ResourceAddressController resourceAddressController;
	
	@Autowired
	private ResourceAddressService resourceAddressService;

	@Autowired
	private ResResRelationController resResRelationController;
	
	@Autowired
	private ResResRelationService resResRelationService;
	
	@Autowired
	private ResourceEmailController resourceEmailController;
	
	@Autowired
	private ResourceEmailService resourceEmailService;
	
	@Autowired
	private ResourceRelationController resourceRelationController;
	
	@Autowired
	private ResourceRelationService resourceRelationService;

	
	@RequestMapping(value = "/{id}", produces = "text/html")
    public String show(@PathVariable("id") Long id, Model uiModel) {
		Model uiModel2 = uiModel;
		addDateTimeFormatPatterns(uiModel2);
		OrgaResource orgaResource = orgaResourceService.findOrgaResource(id);
        uiModel2.addAttribute("orgaresource", orgaResource);
        uiModel2.addAttribute("itemId", id);
        listSourceRelations(id, 0, 5, uiModel2); 
        List<ResourceAddress> resourceAddress = resourceAddressService.findByOrgaResource(orgaResource);
        uiModel2.addAttribute("resourceaddresses", resourceAddress);
        resourceAddressController.addDateTimeFormatPatterns(uiModel2);

         List<ResourceEmail> resourceEmails = resourceEmailService.findByOrgaResource(orgaResource);
        uiModel2.addAttribute("resourceemails", resourceEmails);
        resourceEmailController.addDateTimeFormatPatterns(uiModel2);

        return "orgaresources/show";
    }
    
    
	private String listSourceRelations(Long resourceHolderId,
    		Integer page, 
    		Integer size, 
    		Model uiModel) 
    {
		OrgaResource resource = resourceHolderId!=null?orgaResourceService.findOrgaResource(resourceHolderId):null;
     Page<ResResRelation> resResRelationPage = resResRelationService.searchResResRelation(resource, null, null,page, size);
     Page<ResourceRelation> resourceRelationsPage = resourceRelationService.searchResourceRelation(null, resource, null, page, size);

        uiModel.addAttribute("resresrelations", resResRelationPage.getContent());
        uiModel.addAttribute("resourcerelations", resourceRelationsPage.getContent());
        resourceRelationController.addDateTimeFormatPatterns(uiModel);
        resResRelationController.addDateTimeFormatPatterns(uiModel);
        return "orgaresources/list";
    }

}


